<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
	<meta http-equiv="Content-Type" content="text/html; charset=utf-8">  
	
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions"%>
<script type="text/javascript">
	

	
		$(function() {

			var rec;
			var data;
			$.ajax({  
	            type : "post",  
	            async : false,  
	            url : "${pageContext.request.contextPath}/business/accounting/kMSZJBSJ/dataGrid.shtml",
	            success : function(res) {  
	            	 data = JSON.parse( res ); 
	            	 data=data.rows;
	            }
	               
	        })  
			pzmbdataGrid = $('#dataGrid1').datagrid({
				url : "${pageContext.request.contextPath}/zDPZCWPZMBController/dataGrid.shtml?ZDPZMBID_EQ_NUM="+${id},
				border : false,
				pagination : true,
				pageSize : 20,
				pageList : [ 10, 20, 30, 40, 80 ],
				sortName : 'FLXH',
				sortOrder : 'ASC',
				onClickCell: onClickCell,  
				checkOnSelect : false,
				selectOnCheck : false,
				nowrap : false,
				striped : true,
				rownumbers : true,
				singleSelect : true,
				columns : [ [{
					field : 'ID',
					title : '编号',
					width : 180,
					hidden : true,
					 editor:'text'
				},{
					field : 'FLXH',
					title : '分录序号',
					width : 80,
					sortable : false
				}, {
					field : 'KMBM',
					title : '科目编码',
					width : 80,
					sortable : false
					
				}, {
					field : 'KMMC',
					title : '科目名称',
					width : 80,
					sortable : false,
					editor:{type:'combobox',options:{valueField:'KMBH',textField:'KMMC',data:data,required:false,
						onSelect:function(rec){
							var row = $('#dataGrid1').datagrid('getSelected'); 
							var rowIndex = $('#dataGrid1').datagrid('getRowIndex',row);//获取行号 
							var to=$('#dataGrid2').datagrid('getRows').length
							for(var i=0;i<data.length;i++){
							var lksjdaf=$('#dataGrid2').datagrid('getData').rows[to-i-1];
							if(lksjdaf!=undefined){
							if($('#dataGrid2').datagrid('getData').rows[to-i-1].ID==undefined)
							$('#dataGrid2').datagrid('deleteRow',to-i-1);
							}
							}
							$.ajax({
								url:"${pageContext.request.contextPath}/zDPZCWPZMBController/dataGrid1.shtml?KMBM="+rec.KMBH,
								type:"post",
								success:function(r){
								var re=JSON.parse(r);
								var r=re.rows;
								var r1=r[0];
								var row = $('#dataGrid1').datagrid('getSelected'); 
								if(row.ID==undefined){
								for(var i=0;i<r.length;i++){
								$("#dataGrid2").datagrid("appendRow", {
									FLXH:row.FLXH,
									CWPZMBKMBM:r1.KMBM,
									YYFZ:r1.YYFZ
						        }); 
								}
								
								}
								}
							}) 
							row.KMBM = rec.KMBH;
							row.KMMC = rec.KMMC;
							row.YYFZ=rec.YYFZ;
							row.EYFZ=rec.EYFZ;
							$('#dataGrid1').datagrid('updateRow',{
								index: rowIndex,
								row:row
							});
							
							/* $.ajax({
								url:"",
								type:"post",
								success:function(r){
								$("#dataGrid2").datagrid("insertRow", {
									JSON.parse(r)
						        }); 
								}
							}) */
					      	
						}
			}}
				},{
					field : 'QZFS',
					title : '取值方式',
					width : 80,
					sortable : false,
					 editor:'text'
				}, {
					field : 'JDFX',
					title : '借贷方向',
					width : 80,
					sortable : false,
					 editor:'text'
				}, {
					field : 'ZY',
					title : '摘要',
					width : 80,
					sortable : false,
					 editor:'text'
				}
				
				]],
				onLoadSuccess : function() {
					parent.$.messager.progress('close');
				}
			});
			console.info(dataGrid1);
			
		});
		$(function() {

			fzpzmbdataGrid = $('#dataGrid2').datagrid({
				url : "${pageContext.request.contextPath}/zDPZFZZPZMBController/dataGrid.shtml?ZDPZMBID_EQ_NUM="+${id},
				border : false,
				pagination : true,
				pageSize : 20,
				pageList : [ 10, 20, 30, 40, 80 ],
				sortName : 'FLXH',
				sortOrder : 'ASC',
				AllowSorting :false,
				onClickCell: onClickCell1,  
				checkOnSelect : false,
				selectOnCheck : false,
				nowrap : false,
				striped : true,
				rownumbers : true,
				singleSelect : true,
				columns : [ [{
					field : 'ID',
					title : '编号',
					width : 180,
					hidden : true,
					 editor:'text'
				},{
					field : 'FLXH',
					title : '分录序号',
					width : 80,
					sortable : false
				}, {
					field : 'CWPZMBKMBM',
					title : '科目编码',
					width : 80,
					sortable : false
				},   {
					field : 'YYFZ',
					title : '一元辅助',
					width : 80,
					sortable : false
				},{
					field : 'YYMCGS',
					title : '一元名称公式',
					width : 80,
					sortable : false,
					 editor:'text'
				}, {
					field : 'EYFZ',
					title : '二元辅助',
					width : 80,
					sortable : false,
					 editor:'text'
				} , {
					field : 'EYMCGS',
					title : '二元名称公式',
					width : 80,
					sortable : false,
					 editor:'text'
				} ,{
					field : 'QZFS',
					title : '取值方式',
					width : 80,
					sortable : false,
					 editor:'text'
				}, {
					field : 'JDFX',
					title : '借贷方向',
					width : 80,
					sortable : false,
					 editor:'text'
				}, {
					field : 'ZY',
					title : '摘要',
					width : 80,
					sortable : false,
					 editor:'text'
				}
				
				]],
				onLoadSuccess : function() {
					parent.$.messager.progress('close');
				}
			});
			console.info(dataGrid2);
			
		});
		
		
		$.extend($.fn.datagrid.methods, {
		    editCell : function(jq, param) {
		        return jq.each(function() {
		            var opts = $(this).datagrid('options');
		            var fields = $(this).datagrid('getColumnFields', true).concat(
		                    $(this).datagrid('getColumnFields'));
		            for ( var i = 0; i < fields.length; i++) {
		                var col = $(this).datagrid('getColumnOption', fields[i]);
		                col.editor1 = col.editor;
		                if (fields[i] != param.field) {
		                    col.editor = null;
		                }
		            }
		            $(this).datagrid('beginEdit', param.index);
		            for ( var i = 0; i < fields.length; i++) {
		                var col = $(this).datagrid('getColumnOption', fields[i]);
		                col.editor = col.editor1;
		            }
		        });
		    }
		});
		var editIndex = undefined;
		//结束编辑
		
		function endEditing() {
		    if (editIndex == undefined) {
		        return true;
		    }
		    if ($('#dataGrid1').datagrid('validateRow', editIndex)) {
		        $('#dataGrid1').datagrid('endEdit', editIndex);
		        editIndex = undefined;
		        return true;
		    } else {
		        return false;
		    }
		}
		//单击单元格 
		function onClickCell(index, field) {
		    if (endEditing()) {
		    	var row = $('#dataGrid1').datagrid('getData').rows[index];
			      
		    	if(row.ID!=undefined&&field=='KMMC'){return}
		    	else{
		        $('#dataGrid1').datagrid('selectRow', index).datagrid('editCell', {
		            index : index,
		            field : field
		        });
		    	}
		       
		       
		        editIndex = index;
		    }
		}
		
		function bc(){
	       
		}
		
		var editIndex1 = undefined;
		//结束编辑
		
		function endEditing1() {
		    if (editIndex1 == undefined) {
		        return true;
		    }
		    if ($('#dataGrid2').datagrid('validateRow', editIndex1)) {
		        $('#dataGrid2').datagrid('endEdit', editIndex1);
		        editIndex1 = undefined;
		        return true;
		    } else {
		        return false;
		    }
		}
		//单击单元格 
		function onClickCell1(index, field) {
		    if (endEditing1()) {
		        $('#dataGrid2').datagrid('selectRow', index).datagrid('editCell', {
		            index : index,
		            field : field
		        });
		        editIndex1 = index;
		    }
		}
		
		function bc1(){
			 $('#dataGrid1').datagrid('endEdit', editIndex);

				var a=$('#dataGrid1').datagrid('getChanges');
				
				$.ajax({  
		            type : "post",  
		            async : false,  
		            url : '${pageContext.request.contextPath}/zDPZCWPZMBController/bj.shtml?jsonString='+JSON.stringify($('#dataGrid1').datagrid("getRows"))+'&&id='+${id}, 
		            success : function(data) {  
		            	　　 data = JSON.parse( data ); 
		                if(data.success){  
		                    alert("保存成功");  
		                    $("#dataGrid1").datagrid('reload');  
		                }  
		                else
		                	alert("保存失败");
		            }  
		        })  
	        $('#dataGrid2').datagrid('endEdit', editIndex1);

			var a=$('#dataGrid2').datagrid('getChanges');
			$.ajax({  
	            type : "post",  
	            async : false,  
	            url : '${pageContext.request.contextPath}/zDPZFZZPZMBController/bj.shtml?jsonString='+JSON.stringify($('#dataGrid2').datagrid("getRows"))+'&&id='+${id}, 
	            success : function(data) {  
	            	 data = JSON.parse( data ); 
	                if(data.success){  
	                    alert("保存成功");  
	                    $("#dataGrid2").datagrid('reload');  
	                }
	                else
	                	alert("保存失败");
	            }  
	        })  
		}
		
		function addRow() {
			var aaa=$('#dataGrid1').datagrid('getData');
	    	var b=aaa.total
	    	var c=$('#dataGrid1').datagrid('getData').rows[b-1];
	    	if(c!=undefined)
	    	var d=c.ID;
	    	else{
	    		$("#dataGrid1").datagrid("appendRow", {
		      		FLXH:b+1
		        }); 
	    		return
	    	}
		    if (endEditing()&&d!=undefined) {
		    
		      	$("#dataGrid1").datagrid("appendRow", {
		      		FLXH:b+1
		        }); 
		       
		    }
		    else
		    alert("请先保存");
		}
		function addRow1() {
		    if (endEditing()) {
		     
		      	$("#dataGrid2").datagrid("appendRow", {
		        
		        }); 
		       
		    }
		}
		function de(){
			var aaa=$('#dataGrid1').datagrid('getData');
	    	var b=aaa.total
	    	var c=$('#dataGrid1').datagrid('getData').rows[b-1];
	    	var d=c.ID;
			var row = $('#dataGrid1').datagrid('getSelected');
		if (row&&d!=undefined) {
		         var rowIndex = $('#dataGrid1').datagrid('getRowIndex', row);
		         $('#dataGrid1').datagrid('deleteRow', rowIndex);  
		         $.ajax({  
			            type : "post",  
			            async : false,  
			            url : '${pageContext.request.contextPath}/zDPZCWPZMBController/update.shtml?id='+row.ID+'&FLXH='+row.FLXH+'&ZDPZMBID='+${id}, 
			            success : function(data) {  
			            	 data = JSON.parse( data ); 
			                if(data.success){  
			                    alert("财务凭证分录删除成功");  
			                    $("#dataGrid1").datagrid('reload');  
			                }  
			                else
			                	alert("财务凭证分录删除失败");
			            }  
			        })  
			        $.ajax({  
			            type : "post",  
			            async : false,  
			            url : '${pageContext.request.contextPath}/zDPZFZZPZMBController/update.shtml?FLXH='+row.FLXH+'&id='+row.KMBM+'&ZDPZMBID='+${id}, 
			            success : function(data) {  
			            	 data = JSON.parse( data ); 
			                if(data.success){  
			                    alert("辅助账凭证分录删除成功");  
			                    $("#dataGrid2").datagrid('reload');  
			                }  
			                else
			                	alert("辅助账凭证分录删除失败");
			            }  
			        })  
			        
			        
		 }
		else
		 alert("请先保存再选择要删除的数据");
		}
		function de1(){var row = $('#dataGrid2').datagrid('getSelected');
		if (row) {
		         var rowIndex = $('#dataGrid2').datagrid('getRowIndex', row);
		         $('#dataGrid2').datagrid('deleteRow', rowIndex); 
		         $.ajax({  
			            type : "post",  
			            async : false,  
			            url : '${pageContext.request.contextPath}/zDPZFZZPZMBController/delete.shtml?id='+row.ID, 
			            success : function(data) { 
			            	 data = JSON.parse( data ); 
			                if(data.success){  
			                    alert("删除成功");  
			                    $("#dataGrid2").datagrid('reload');  
			                }  
			                else
			                	alert("删除失败");
			            }  
			        })  
		 }
		else
			 alert("未选择要删除的数据");
		}


		function sf() {
			pzmbdataGrid.datagrid('reload', $.serializeObject($('#sf')));
		}

		function sf1() {
			fzpzmbdataGrid.datagrid('reload', $.serializeObject($('#sf1')));
		}
	</script>
		<div style="font-weight: 800;">财务凭证分录</div>
		<!-- <div class="easyui-layout" data-options="fit : true,border : false">
		<div data-options="title:'',border:false" style="height: 35px; overflow: hidden;" >
			<form id="sf">
				<table class="form_view_border" bordercolordark="#FFFFFF" bordercolorlight="#45b97c" border="px" cellpadding="0" cellspacing="0" style="">
					<tr>
						<th class="panel-header"><nobr>分录序号</nobr></th>
						<td>
							<input style="width: 100px;height: 27px;" class="easyui-text"
							name="FLXH_CONTAINS_STR" 
							class="span2" />
						</td>
						<th class="panel-header"><nobr>科目编码</nobr></th>
						<td>
							<input style="width: 100px;height: 27px;" class="easyui-text"
							name="KMBM_CONTAINS_STR" 
							class="span2" />
						</td>
						<th class="panel-header"><nobr>科目名称</nobr></th>
						<td>
							<input style="width: 100px;height: 27px;" class="easyui-text"
							name="KMMC_EQ_STR" 
							class="span2" />
						</td>
						<td>
						  <a  onclick=sf(); class="l-btn" id="chaxun_search_button"><span class="l-btn-left">
          <span class="l-btn-text icon-search l-btn-icon-left">查询</span></span></a>
						</td>
					</tr> -->
			<!-- 	</table>
			</form>
		</div>
	</div> -->

	 <a class="l-btn"  onclick=addRow()><span class="l-btn-left">
          <span class="l-btn-text " >新增</span></span></a>
<a class="l-btn" onclick=de();><span class="l-btn-left">
          <span class="l-btn-text ">删除</span></span></a>
	<table  id="dataGrid1" style="height:200px;">
	</table>
	<br>
	<br>
	<div style="font-weight: 800;">辅助账凭证分录</div>
		<!-- <div class="easyui-layout" data-options="fit : true,border : false">
		<div data-options="title:'',border:false" style="height: 35px; overflow: hidden;" >
			<form id="sf1">
				<table class="form_view_border" bordercolordark="#FFFFFF" bordercolorlight="#45b97c" border="px" cellpadding="0" cellspacing="0" style="">
					<tr>
						<th class="panel-header"><nobr>分录序号</nobr></th>
						<td>
							<input style="width: 100px;height: 27px;" class="easyui-text"
							name="FLXH_CONTAINS_STR" 
							class="span2" />
						</td>
						<th class="panel-header"><nobr>科目编码</nobr></th>
						<td>
							<input style="width: 100px;height: 27px;" class="easyui-text"
							name="CWPZMBKMBM_CONTAINS_STR" 
							class="span2" />
						</td>
						<td>
						  <a  onclick=sf1(); class="l-btn" id="chaxun_search_button"><span class="l-btn-left">
          <span class="l-btn-text icon-search l-btn-icon-left">查询</span></span></a>
						</td>
					</tr>
				</table>
			</form>
		</div>
	</div> -->
<!-- 	<a class="l-btn" onclick=addRow1();><span class="l-btn-left">
          <span class="l-btn-text ">新增</span></span></a>
	<a class="l-btn" onclick=de1();><span class="l-btn-left">
          <span class="l-btn-text ">删除</span></span></a> -->
	<table id="dataGrid2" style="height:200px;">
	</table>
	<a class="l-btn" onclick=bc1();><span class="l-btn-left">
          <span class="l-btn-text ">保存</span></span></a>
	




